package com.cqh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqh.common.R;
import com.cqh.dto.DishDto;
import com.cqh.entity.Category;
import com.cqh.entity.Dish;
import com.cqh.entity.DishFlavor;
import com.cqh.entity.Setmeal;
import com.cqh.service.CategotyService;
import com.cqh.service.DishFlavorService;
import com.cqh.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
     @Autowired
    DishFlavorService dishFlavorService;
     @Autowired
    DishService dishService;
     @Autowired
    CategotyService categotyService;
    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PutMapping
     public R<String> update(@RequestBody DishDto dishDto){
       log.info(dishDto.toString());
       dishService.updateWithFlavor(dishDto);
       return  R.success("新增成功");
     }
    @PostMapping
    public R<String> saveDs(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return  R.success("新增成功");
    }
     @GetMapping("/page")
     public R<Page> page(int page,int pageSize,String name){
        Page<Dish> dishPage=new Page<>(page,pageSize);
         Page<DishDto> pageInfo=new Page<>(page,pageSize);
         LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper();
         lambdaQueryWrapper.like(name!=null,Dish::getName,name);
         lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
         dishService.page(dishPage,lambdaQueryWrapper);
         BeanUtils.copyProperties(dishPage,pageInfo,"records");
         List<Dish> record=dishPage.getRecords();
         List<DishDto> dishDtos= record.stream().map((item)->{
             DishDto dishDto=new DishDto();
             BeanUtils.copyProperties(item,dishDto);
             Long categoryId=item.getCategoryId();
            Category  category= categotyService.getById(categoryId);
            String  categoryName=category.getName();
            dishDto.setCategoryName(categoryName);
            return dishDto;
         }).collect(Collectors.toList());
        pageInfo.setRecords(dishDtos);

         return R.success(pageInfo);
     }
   @GetMapping("{id}")
     public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto=dishService.getByidWithFlavor(id);
        return  R.success(dishDto);
   }
   @GetMapping("/list")
    public R<List<DishDto>> list( Dish dish){
        log.info("点餐信息{}",dish);
        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
         List<Dish> dishes= dishService.list(lambdaQueryWrapper);

         List<DishDto> dishDtos=dishes.stream().map((item)->{
             DishDto dishDto=new DishDto();
             BeanUtils.copyProperties(item,dishDto);
             Long categoryId=item.getCategoryId();
             Category category=categotyService.getById(categoryId);
             if (category!=null){
                 dishDto.setCategoryName(category.getName());
             }
             Long id=item.getId();
             LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
             queryWrapper.eq(DishFlavor::getDishId,id);
           List<DishFlavor> dishFlavors=  dishFlavorService.list(queryWrapper);
           dishDto.setFlavors(dishFlavors);
           return dishDto;
         }).collect(Collectors.toList());

        return R.success(dishDtos);
    }

    @PostMapping("/status/1")
    public R<String> status1(@RequestParam List<Long> ids){

        List<Dish> list=dishService.listByIds(ids);
        list.stream().map((item)->{

            item.setStatus(1);
            return item;
        }).collect(Collectors.toList());
        dishService.saveOrUpdateBatch(list);
        return R.success("改变成功");
    }
    @PostMapping("/status/0")
    public R<String> status0(@RequestParam List<Long> ids){

        List<Dish> list=dishService.listByIds(ids);
        list.stream().map((item)->{

            item.setStatus(0);
            return item;
        }).collect(Collectors.toList());
        dishService.saveOrUpdateBatch(list);
        return R.success("改变成功");
    }

}
